var com = {};

Array.prototype.indexOf = function (val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == val) return i;
    }
    return -1;
};

Array.prototype.remove = function (val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};

com.regex = {
    email: function (value) {
        return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
    },
    phone: function (value) {
        // return /^((\(\d{2,3}\))|(\d{3}\-))?1\d{10}$/.test(value);
        return /^1\d{10}$/.test(value);
    },
    account: function (value) {
        return /^[\u0391-\uFFE5\w]+$/.test(value);
    },
    password: function (value) {
        return /^[0-9A-Za-z]{6,12}$/i.test(value);
    },
    idCard: function (value) {
        var isDateTime = function (format, reObj) {
            format = format || 'yyyy-MM-dd';
            var input = this, o = {}, d = new Date();
            var f1 = format.split(/[^a-z]+/gi), f2 = input.split(/\D+/g), f3 = format.split(/[a-z]+/gi), f4 = input.split(/\d+/g);
            var len = f1.length, len1 = f3.length;
            if (len != f2.length || len1 != f4.length) return false;
            for (var i = 0; i < len1; i++) if (f3[i] != f4[i]) return false;
            for (var i = 0; i < len; i++) o[f1[i]] = f2[i];
            var expiryDate = 1900;
            if (o.yyyy < expiryDate) {
                return false;
            }
            o.yyyy = s(o.yyyy, o.yy, d.getFullYear(), 9999, 4);
            o.MM = s(o.MM, o.M, d.getMonth() + 1, 12);
            o.dd = s(o.dd, o.d, d.getDate(), 31);
            o.hh = s(o.hh, o.h, d.getHours(), 24);
            o.mm = s(o.mm, o.m, d.getMinutes());
            o.ss = s(o.ss, o.s, d.getSeconds());
            o.ms = s(o.ms, o.ms, d.getMilliseconds(), 999, 3);
            if (o.yyyy + o.MM + o.dd + o.hh + o.mm + o.ss + o.ms < 0) return false;
            if (o.yyyy < 100) o.yyyy += (o.yyyy > 30 ? 1900 : 2000);
            d = new Date(o.yyyy, o.MM - 1, o.dd, o.hh, o.mm, o.ss, o.ms);
            var reVal = d.getFullYear() == o.yyyy && d.getMonth() + 1 == o.MM && d.getDate() == o.dd && d.getHours() == o.hh && d.getMinutes() == o.mm && d.getSeconds() == o.ss && d.getMilliseconds() == o.ms;
            return reVal && reObj ? d : reVal;

            function s(s1, s2, s3, s4, s5) {
                s4 = s4 || 60, s5 = s5 || 2;
                var reVal = s3;
                if (s1 != undefined && s1 != '' || !isNaN(s1)) reVal = s1 * 1;
                if (s2 != undefined && s2 != '' && !isNaN(s2)) reVal = s2 * 1;
                return (reVal == s1 && s1.length != s5 || reVal > s4) ? -10000 : reVal;
            }
        };

        if (value.length == 18 && 18 != value.length) return false;
        var number = value.toLowerCase();
        var d, sum = 0, v = '10x98765432', w = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2], a = '11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82,91';
        var re = number.match(/^(\d{2})\d{4}(((\d{2})(\d{2})(\d{2})(\d{3}))|((\d{4})(\d{2})(\d{2})(\d{3}[x\d])))$/);
        if (re == null || a.indexOf(re[1]) < 0) return false;
        if (re[2].length == 9) {
            number = number.substr(0, 6) + '19' + number.substr(6);
            d = ['19' + re[4], re[5], re[6]].join('-');
        } else d = [re[9], re[10], re[11]].join('-');
        if (!isDateTime.call(d, 'yyyy-MM-dd')) return false;
        for (var i = 0; i < 17; i++) sum += number.charAt(i) * w[i];
        return (re[2].length == 9 || number.charAt(17) == v.charAt(sum % 11));
    },
    username: function (value) {
        //return /[\u4E00-\u9FA5]{2,5}(?:·[\u4E00-\u9FA5]{2,5})*/i.test(value);
        return /^([\u4e00-\u9fa5·]){2,7}$/i.test(value);
    }
};

//menu
$(".menu .dropdown").hover(
        function () {
            $(this).find(".dropdown-menu").show();
        },
        function () {
            $(this).find(".dropdown-menu").hide();
        }
    );

$('#logout').click(function () {
    layer.confirm('您确定要退出本次登录吗?', {}, function () {
        $.cookie('my_gxrczc_userName', null, { domain: '.gxrczc.com' });
        window.location = '/login/logout';
    });
});


//tiptype
var mytype = function (msg, o, cssctl) {
    if (!o.obj.is("form")) {
        var objtip = o.obj.parents("div").find(".Validform_checktip");
        cssctl(objtip, o.type);
        objtip.text(msg);

        var infoObj = o.obj.parents("div").find(".info");
        if (o.type == 2) {
            infoObj.fadeOut(200);
        } else {
            if (infoObj.is(":visible")) { return; }
            var left = o.obj.offset().left,
                top = o.obj.offset().top;

            infoObj.css({
                left: left + 486,
                top: top - 45
            }).show().animate({
                top: top - 35
            }, 200);
        }

    }
}

var mytype1 = function (msg, o, cssctl) {
    if (!o.obj.is("form")) {
        var objtip = o.obj.parents("div").find(".Validform_checktip");
        cssctl(objtip, o.type);
        objtip.text(msg);

        var infoObj = o.obj.parents("div").find(".info");
        if (o.type == 2) {
            infoObj.fadeOut(200);
        } else {
            if (infoObj.is(":visible")) { return; }
            var left = o.obj.offset().left,
                top = o.obj.offset().top;

            infoObj.css({
                left: left + 150,
                top: top - 45
            }).show().animate({
                top: top - 35
            }, 200);
        }

    }
}


function checkUnitCode(myobject, hasOldValue) {
    var unitName = '';
    for (var i = 0; i < data.org.length; i++) {
        var org = data.org[i];
        if (myobject.CertUnit == org.label) {
            if (myobject.CertUnitCode != org.value) {
                $('#CertUnitCode').val(org.value);
            }
            unitName = myobject.CertUnit;
            break;
        }
    }

    if (!unitName) {
        $('#btn-confirm').attr("disabled", true);

        $.ajax({
            async: false,
            type: 'post',
            url: '/api/ct/register/PostRegUnit',
            data: "unitName=" + myobject.CertUnit,
            success: function (r) {
                //if (r.UnitName && r.UnitCode) {
                //    unitName = '<span style="color:red;">(您输入的授予单位不存在，请从下面的列表中选择)</span><br/>' +r.UnitName;
                //    $('#CertUnit').val(r.UnitName);
                //    $('#CertUnitCode').val(r.UnitCode);
                //    $('#btn-confirm').attr("disabled",false);
                //}

                if (r) {
                    var tempName = '';
                    if (hasOldValue && hasOldValue === true) {
                        tempName = '<input type="radio" class="raidUnitInfo" name="raidUnitInfo" value="' + myobject.CertUnitCode + '" /><span>' + myobject.CertUnit+ '</span><br/>';
                    }
                    for (var i = 0; i < r.length; i++) {
                        tempName += '<input type="radio" class="raidUnitInfo" name="raidUnitInfo" value="' + r[i].value + '" /><span>' + r[i].key + '</span><br/>';
                    }

                    unitName = '<span style="color:red;">(您输入的授予单位不存在，请从下面的选项中选择)</span><br/>' + tempName;
                    setTimeout(function () {
                        $($("input[name='raidUnitInfo']")[0]).prop("checked", true);
                    }, 0);

                    if (!hasOldValue) {
                        $('#CertUnit').val(r[0].key);
                        $('#CertUnitCode').val(r[0].value);
                    }
                    
                    $('#btn-confirm').attr("disabled", false);
                } else {
                    $('#CertUnit').val('');
                    $('#CertUnitCode').val('');
                    unitName = '<span style="color:red;">(获取授予单位失败，请重新输入选择！)</span><br/>';
                }
            },
            error: function (e) {
                layer.alert(e.responsetext, {
                    icon: 5,
                });
            }
        });
    }

    return unitName;
}

function checkUnitCode2(unitName, unitCode) {
    alert(unitName + unitCode);
}